package com.life.ai.server.framework.config;

import cn.dev33.satoken.config.SaTokenConfig;
import com.life.ai.server.framework.config.properties.SecurityProperties;
import com.life.ai.server.framework.satoken.TokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * SaTokenConfig
 *
 * @author kylewka
 * @since 2023/9/20
 */
@Slf4j
@Configuration
public class SecurityConfig implements WebMvcConfigurer {

    @Resource
    private SecurityProperties securityProperties;

    /**
     * 此配置会与 application.yml 中的配置合并 （代码配置优先）
     *
     * @param config Sa-Token 参数配置
     */
    @Autowired
    public void configSaToken(SaTokenConfig config) {
        config.setIsLog(false);
        config.setIsPrint(false);
        config.setIsReadBody(false);
        config.setIsReadCookie(false);
    }

    /**
     * 注册sa-token的拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册路由拦截器，自定义验证规则
        registry.addInterceptor(new TokenInterceptor()).addPathPatterns("/**")
                // 排除不需要拦截的路径
                .excludePathPatterns(securityProperties.getIgnoreUrls());
    }
}
